/**
 * 本代码设计用于增删改查四种操作的基函数
 * 要使用这些基函数来实现增删改查必须要重写该函数的某些内容
 * 比如要对表department进行读取操作那就先要重写get函数
 * get(id)
 */

var conn = require('../util/conn');
var Q = require('q');
//function get(connection,id,tableName){
function get(tableName){
    return function (id){
        var connection = conn.getConnection();
        var deferred = Q.defer();
        if(id){
            var query = connection.query('select * from '+tableName+' where id = '+id,function(err,rows,fields){
                if(err){
                    deferred.reject(err);
                }else {
                    if (rows.length > 0) {
                        deferred.resolve(rows[0]);
                    } else {
                        deferred.resolve({});
                    }
                }
            });
            console.log(query.sql);
        }else {
            deferred.resolve({error:'id is not exitst!'});
        }
        connection.end();
        return deferred.promise;
    }
}
//var myget = get('department');
//myget(conn.getConnection(),10).then(console.log);

function save(tableName){
    return function (model){
        var connection = conn.getConnection();
        var deferred = Q.defer();
        model.id = null;//不管id有没有值都直接赋值为null

        var query = connection.query('insert into '+tableName+' set ?',model,function(err,result){
            if(err){
                deferred.reject(err);
            }else {
                deferred.resolve(result);
            }
        });
        console.log(query.sql);
        connection.end();
        return deferred.promise;
    }
}

function update(tableName){
    return function (model){
        var connection = conn.getConnection();
        var deferred = Q.defer();
        if(model.id){
            var query = connection.query('update '+tableName+' set ? where id = ' + model.id,model,function(err,result){
                if(err){
                    deferred.reject(err);
                }else {
                    deferred.resolve(result);
                }
            });
            console.log(query.sql);
        }else{
            deferred.reject({error:'id is not exist!'});
        }
        connection.end();
        return deferred.promise;
    }
}

function remove(tableName){
    return function (id){
        var connection = conn.getConnection();
        var deferred = Q.defer();
        if(id){
            var query = connection.query('delete from '+tableName+' where id = ' + id,function(err,result){
                if(err){
                    deferred.reject(err);
                }else {
                    deferred.resolve(result);
                }
            });
            console.log(query.sql);
        }else{
            deferred.reject({error:'id is not exist!'});
        }
        connection.end();
        return deferred.promise;
    }
}
module.exports = function(tableName){
    return {
        get:get(tableName),
        save:save(tableName),
        update:update(tableName),
        remove:remove(tableName)
    }
};
/****
 * 一些想法，对于crud四种操作连接的打开和关闭以及defer都是公用的
 * 那么是不是可以在实例化这四种操作时将这些公用的操作进行注入
 * 比如get操作
 * 在department中
 * get(id){
 *  connection = conn.getConnection();
 *  connection.connect();
 *  var deferred = Q.defer();
 *  var retf =  crud.get('department',connection,deferred)
 *  var ret = retf(id)//return a promise
 *  //上面的代码可以不可以直接将department,connection,deferred一起传入
 *  //后面的调用只需要写retf()
 *  connection.end();
 *  return ret
 *  }//还有个问题 如果一直都动态生成函数会不会有性能问题
 *  第二种版本
 *
 *  //crud get template
 *  get(conn,tablename){
 *      connection = conn.getConnection();
 *      connection.connect();
 *      return function(id){
 *          ...
 *          connection.query(....)
 *          ...
 *      }
 *  }
 *  //in department
 *  conn = ....
 *  get = crud.get(conn,'department')
 *  //这里的get函数只生成一次，而上面的函数每次调用都要生成一个临时函数
 *
 *
 ****/